package com.leaf.lowcode.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.API.DoorGateApi;
import com.leaf.lowcode.entity.door_control.GetRequest;
import com.leaf.lowcode.entity.door_control.Readerdoorstate;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 上传读卡器状态数据
 */

@Component
public class ReaderDoorUp {
   @Scheduled(initialDelay = 20 * 1000, fixedRate = 15 * 1000)
    public void execute() {
        System.out.println("=======获取门禁读卡器数据数据===========");
        DoorGateApi doorGateApi = new DoorGateApi();
        GetRequest getRequest = new GetRequest();
        Readerdoorstate readerdoorstate=null;
        ArrayList<Readerdoorstate> readerdoorstateArrayList = new ArrayList<Readerdoorstate>();
        getRequest.setPageNo(1);
        getRequest.setPageSize(10);
        String readerget = doorGateApi.Readerget(getRequest);
        JSONObject jsonObject = JSONObject.parseObject(readerget);
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
     //   System.out.println("读卡器数据" + readerget);
        for (int i = 0; i < list.size(); i++) {
            JSONObject json1 = (JSONObject) list.get(i);
            // System.out.println("json"+"第几"+i+json1);
            readerdoorstate= new Readerdoorstate();
            Field[] fields = readerdoorstate.getClass().getDeclaredFields();
            for (Field field : fields) {
                //System.out.println(field.getName());
                Iterator<String> iterator = json1.keySet().iterator();
                while (iterator.hasNext()) {
                    String name = iterator.next();
                    if (field.getName().equals(name)) {
                        Object o = json1.get(name);
                        if (field.getType().toString().equals("class java.lang.String")) {
                            String o1;
                            if (o == null) {
                                o1 = "";

                            } else {
                                o1 = o.toString();
                            }
                            try {
                                field.setAccessible(true);
                                field.set(readerdoorstate, o1);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        } else if (field.getType().toString().equals("class java.lang.Integer")) {
                            Integer o1 = null;
                            o1 = Integer.valueOf(o.toString());
                            try {
                                field.setAccessible(true);
                                field.set(readerdoorstate, o1);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        } else {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
                            Date ts = null;
                            try {
                                ts = sdf.parse((String) o);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            try {
                                field.setAccessible(true);
                                field.set(readerdoorstate, ts);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }

            }
            readerdoorstateArrayList.add(readerdoorstate);
        }

        Iterator<Readerdoorstate> iterator = readerdoorstateArrayList.iterator();
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        while (iterator.hasNext()) {
            Readerdoorstate readerdoorstatesend = iterator.next();
            if (subDeviceDTOList != null) {
                for (SubDeviceDTO dto : subDeviceDTOList) {
                    String pid = dto.getPid();
                    String sn = dto.getSn();
                    String din = dto.getDin();
                    if (readerdoorstatesend.getIndexCode().equals(sn)) {
                      //  System.out.println("======================门禁卡========================"+readerdoorstatesend.getIndexCode());
                        JSONObject standardData = new JSONObject();
                        standardData.put("reportTs", System.currentTimeMillis());
                        JSONObject profile = new JSONObject();
                        profile.put("modelID", "200418");
                        profile.put("poiCode", "w0703000");
                        standardData.put("profile", profile);
                        WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                        JSONObject properties = new JSONObject();
                        JSONObject propertie = new JSONObject();
                        propertie.put("deviceType", readerdoorstatesend.getDeviceType());
                        propertie.put("indexCode", readerdoorstatesend.getIndexCode());
                        propertie.put("online", readerdoorstatesend.getOnline());
                        propertie.put("collectTime", readerdoorstatesend.getCollectTime().toString());
                        properties.put("readerinfo", propertie);
                        standardData.put("properties", properties);
                        //standardData.put("properties", "{}");
                        try {
                         //   System.out.println("上传数据");
                            String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                         //   System.out.println("发送设备运行时事件数据==" + devRunEvent);
                        //    System.out.println("--------------------------------开始传输设备运行时事件数据");
                            wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                        } catch (Exception e) {
                            System.out.println("设备运行时事件数据传输出现异常");
                        }
                    }
                }
            }
        }
    }
}

